package com.wgu.crawl.web;

import com.wgu.common.global.Result;
import com.wgu.crawl.CrawlBook;
import com.wgu.crawl.CrawlType;
import com.wgu.crawl.service.CrawlBookService;
import com.wgu.crawl.service.CrawlService;
import com.wgu.crawl.service.CrawlTypeService;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 全本小说网
 * 爬虫控制器
 * @Author: w
 * @Date: 2019/6/27 14:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/crawl")
public class CrawlController {
    @Resource
    private CrawlService crawlService;
    @Resource
    private CrawlTypeService crawlTypeService;
    @Resource
    private CrawlBookService crawlBookService;

    /**
     * 爬取书籍类型
     * @param url 全本小说网的网址
     * @return
     */
    @GetMapping("/type")
    public List<CrawlType> type(String url){
        return crawlService.crawlType(url);
    }

    /**
     * 爬取书籍
     * @return
     */
    @GetMapping("/book")
    public Result book(){
        List<CrawlType> list = crawlTypeService.getAllType();
        list.forEach((x)->crawlService.crawlBook(x));
        return Result.ok();
    }

    /**
     * 根据分页进行爬虫章节内容
     * @param pageable
     * @return
     */
    @GetMapping("/chapter")
    public Result content(Pageable pageable){
        List<CrawlBook> list = crawlBookService.page(pageable);
        list.forEach((x)->crawlService.crawlChapter(x));
        return Result.ok();
    }
}
